#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from SinaWeiboAPI.sinaAPI import SinaAPI
from SinaWeiboAPI import oauth
import unittest
import simplejson as json


class WeiboTest(unittest.TestCase):
    """
    Test for the weibo operation
    Statuses/show
    User/statuses/id
    Statuses/update
    Statuses/upload
    Statuses/destroy
    Statuses/repost
    Statuses/comment
    Statuses/comment_destroy/:id
    Statuses/comment/destroy_batch
    Statuses/reply
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 10)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)

    def testStatusesShow(self):
        """test for statuses/show"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.show_statuses(id=10736464264)
        self.assertTrue(res is not None)
        print res.text
  
    def testStatusesUpdate(self):
        """test for statuses/update"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        message = 'python sina API test! update a status! 发送一条微博!'
        res = testAPI.update_statuses(status=message, lat=1.342569, \
                                     long=103.679695)
        self.assertTrue(res is not None)
        print res.text
  
    def testStatusesUpload(self):
        """test for statuses/upload"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        message = 'python sina API test! upload a status! 发送一条图片微博!'
        res = testAPI.upload_statuses(status=message, lat=1.342569, \
                                     long=103.679695, pic='soy.jpg')
        self.assertTrue(res is not None)
        print res.text
  
    def testStatusesDestroy(self):
        """test for statuses/destroy"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.destroy_statuses(id=10796375805)
        self.assertTrue(res is not None)
        print res.text

    def testStatusesRepost(self):
        """test for statuses/repost"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        message = 'python sina API test! repost a status! 转发一条微博!\
                哥就是转发一下!'
        res = testAPI.repost_statuses(id=10916443380, status=message, \
                                     is_comment=3)
        self.assertTrue(res is not None)
        print res.text

    def testStatusesComment(self):
        """test for statuses/comment"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        message = 'python sina API test! comment a status!删除用2'
        res = testAPI.comment_statuses(id=10736464264, \
                                       comment=message)
        self.assertTrue(res is not None)
        print res.text
        print res.id
        
    def testStatusesCommentDestroy(self):
        """test for statuses/comment_destroy"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.comment_destroy_statuses(id=11969884203)
        self.assertTrue(res is not None)
        print res.text

    def testStatusesCommentDestroyBatch(self):
        """test for statuses/comment/destroy_batch"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        cids='11970569019,11970595894'
        res = testAPI.comment_destroy_batch_statuses(ids=cids)
        self.assertTrue(res is not None)

    def testStatusesReply(self):
        """test for statuses/reply"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        message = 'python sina API test! reply a comment! 回复一条评论!\
                谢谢!'
        res = testAPI.reply_statuses(cid=11842225586, comment=message, \
                                     id=10916443380)
        self.assertTrue(res is not None)
        print res.text

if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(WeiboTest('testStatusesReply'))
    unittest.TextTestRunner().run(fast)
